/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package TRIRzadeve;

import com.hp.hpl.jena.query.*;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

/**
 *
 * @author Irena
 */
public class RDFPovprasevanja {
    
    private Date date = new Date();
    private DateFormat datum = new SimpleDateFormat("dd.MM.yyyy-HH.mm.ss");
    private QueryExecution qe;
    
    public ArrayList<Vreme> povprasevanjeVrniVsaMesta(ArrayList<Model> model) {
        ArrayList<Model> izbranaMesta = new ArrayList<Model>();
        int korak = model.size()/17;
        for(int i = korak - 1; i < model.size(); i = i + korak) {
                izbranaMesta.add(model.get(i));
        }
        
        
        String queryString = 
                "PREFIX vreme: <ftp://ftp.ribogojnica-pozeg.si/> " +
                "SELECT ?x ?mesto ?hitrostVetra ?smerVetra ?slika ?vlaznost ?temperatura ?stanje ?datum" +
                "WHERE {" +
                "      ?x vreme:mesto ?mesto." +
                "      ?x vreme:hitrostVetra ?hitrostVetra." +
                "      ?x vreme:smerVetra ?smerVetra." +
                "      ?x vreme:slika ?slika." +
                "      ?x vreme:vlaznost ?vlaznost." +
                "      ?x vreme:temperatura ?temperatura." +
                
                "      ?x vreme:stanje ?stanje." +
                
                "      ?x vreme:datum ?datum" +
                "      }";
        Query query = QueryFactory.create(queryString);
        ArrayList<Vreme> vremeIzbranegaMesta = new ArrayList<Vreme>();
        for(Model m : izbranaMesta) {
            qe = QueryExecutionFactory.create(query, m);
            ResultSet results = qe.execSelect();
            for(;results.hasNext();) {
                QuerySolution soln = results.nextSolution();
                System.out.print(soln.get("x") + " " + soln.get("stanje"));
                Vreme v = new Vreme();
                v.setMesto(soln.get("mesto").toString());
                v.setTemperatura(soln.get("temperatura").toString());
                v.setDatum(soln.get("x").toString().substring(soln.get("x").toString().indexOf(v.getMesto()) + v.getMesto().length(), soln.get("x").toString().length()));
                v.setHitrostVeter(soln.get("hitrostVetra").toString());
                v.setSlika(soln.get("slika").toString());
                v.setSmerVeter(soln.get("smerVetra").toString());
                v.setStanje(soln.get("stanje").toString());
                v.setVlaznost(soln.get("vlaznost").toString());
                vremeIzbranegaMesta.add(v);
            }
            //temp.write(System.out);
            //ResultSetFormatter.out(System.out, results, query);
        }
        
       

        
        qe.close();
        return vremeIzbranegaMesta;
    }
    
    public ArrayList<Vreme> povprasevanjeVrniMesto(ArrayList<Model> model, String mesto) {
        // Create a new query
        String queryString = 
                "PREFIX vreme: <ftp://ftp.ribogojnica-pozeg.si/> " +
                "SELECT ?x ?mesto ?hitrostVetra ?smerVetra ?slika ?vlaznost ?temperatura ?stanje ?datum" +
                "WHERE {" +
                "      ?x vreme:mesto \"" + mesto + "\" . " +
                "      ?x vreme:mesto ?mesto." +
                "      ?x vreme:hitrostVetra ?hitrostVetra." +
                "      ?x vreme:smerVetra ?smerVetra." +
                "      ?x vreme:slika ?slika." +
                "      ?x vreme:vlaznost ?vlaznost." +
                "      ?x vreme:temperatura ?temperatura." +
                
                "      ?x vreme:stanje ?stanje." +
                
                "      ?x vreme:datum ?datum" +
                "      }";
        Query query = QueryFactory.create(queryString);
        ArrayList<Vreme> vremeIzbranegaMesta = new ArrayList<Vreme>();
        for(Model m : model) {
            qe = QueryExecutionFactory.create(query, m);
            ResultSet results = qe.execSelect();
            for(;results.hasNext();) {
                QuerySolution soln = results.nextSolution();
                System.out.print(soln.get("x") + " " + soln.get("stanje"));
                Vreme v = new Vreme();
                v.setMesto(soln.get("mesto").toString());
                v.setTemperatura(soln.get("temperatura").toString());
                v.setDatum(soln.get("x").toString().substring(soln.get("x").toString().indexOf(mesto) + mesto.length(), soln.get("x").toString().length()));
                v.setHitrostVeter(soln.get("hitrostVetra").toString());
                v.setSlika(soln.get("slika").toString());
                v.setSmerVeter(soln.get("smerVetra").toString());
                v.setStanje(soln.get("stanje").toString());
                v.setVlaznost(soln.get("vlaznost").toString());
                vremeIzbranegaMesta.add(v);
            }
            //temp.write(System.out);
            //ResultSetFormatter.out(System.out, results, query);
        }
        
       

        
        qe.close();
        return vremeIzbranegaMesta;
        
    }

}
